package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignProcedureDto;
import com.guigu.erp.mapper.ManufactureDesignProcedureMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.ManufactureDesignProcedure;
import com.guigu.erp.pojo.ManufactureDesignProcedureDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.ManufactureDesignProcedureDetailsService;
import com.guigu.erp.service.ManufactureDesignProcedureService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Lenovo
 */
@Service
public class ManufactureDesignProcedureServiceImpl extends ServiceImpl<ManufactureDesignProcedureMapper, ManufactureDesignProcedure>
        implements ManufactureDesignProcedureService {
    @Autowired
    private ManufactureDesignProcedureMapper manufactureDesignProcedureMapper;

    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private DesignFileService designFileService;

    @Override
    public String getMaxDesignId() {
        ManufactureDesignProcedure manufactureDesignProcedure=manufactureDesignProcedureMapper.queryMaxProcedure();
        if(manufactureDesignProcedure==null){
            return null;
        }
        // 获取设计单编号
        String designId = manufactureDesignProcedure.getDesignId();
        return designId;
    }

    /**
     * 完成生产工序设计单的制定
     * @param designProcedureDto
     */
    @Override
    public void makeDesignProcedure(DesignProcedureDto designProcedureDto) {
        //1.插入生产工序设计单
        this.save(designProcedureDto);

        //2.插入生产工序设计单明细
        List<ManufactureDesignProcedureDetails> details=designProcedureDto.getDetails();

        for (int i=0;i<details.size();i++){
            ManufactureDesignProcedureDetails detail=details.get(i);
            //设置每条明细id为空
            detail.setId(null);
            //获取生产工序设计单的id
            Integer parentId=designProcedureDto.getId();
            //设置父级序号
            detail.setParentId(parentId);
            //设置工序序号
            detail.setDetailsNumber(i+1);
            //设置当前工序物料标志
            detail.setDesignModuleTag("D002-0");
            //设置当前工序物料变更标志
            detail.setDesignModuleChangeTag("D003-0");
            //插入生产工序明细
            manufactureDesignProcedureDetailsService.save(detail);

            //3.修改生产工序的设计标志
            String productId=designProcedureDto.getProductId();
            QueryWrapper<DesignFile> wrapper=new QueryWrapper<>();
            wrapper.eq("product_id",productId);
            DesignFile designFile=designFileService.getOne(wrapper);
            designFile.setDesignProcedureTag("G001-1");
            designFileService.updateById(designFile);
        }
    }

    /**
     * 按条件分页查询物料组成设计的未复核
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<ManufactureDesignProcedure> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("thirdKindId",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加2个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-0");
        // 删除状态：未删除
        queryWrapper.eq("change_tag","B002-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> manufactureDesignProcedureList =this.list(queryWrapper);
        PageInfo<ManufactureDesignProcedure> manufactureDesignProcedurePageInfo = new PageInfo<>(manufactureDesignProcedureList);

        //将pageinfo 转换成 pageutils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(manufactureDesignProcedurePageInfo.getList());
        pageUtils.setTotal(manufactureDesignProcedurePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> procedureListPage(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<ManufactureDesignProcedure>();
        //审核状态：审核通过
        wrapper.eq("check_Tag", "S001-1");
        //变更状态: 未变更
        wrapper.eq("change_Tag","B002-0");
        if(vo != null) {
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureDesignProcedure> manufactureDesignProcedures = this.list(wrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<ManufactureDesignProcedure>(manufactureDesignProcedures);
        //PageInfo转PageUtils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<ManufactureDesignProcedure>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    /**
     * 按条件分页查询物料设计单未设计的
     * @param vo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageUtils<ManufactureDesignProcedure> moduleRecheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("thirdKindId",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加2个条件
        // 审核状态：审核通过
        queryWrapper.eq("check_tag","S001-1");
        // 工序物料设计状态：未设计
        queryWrapper.eq("design_module_tag","G002-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> manufactureDesignProcedureList =this.list(queryWrapper);
        PageInfo<ManufactureDesignProcedure> manufactureDesignProcedurePageInfo = new PageInfo<>(manufactureDesignProcedureList);

        //将pageinfo 转换成 pageutils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(manufactureDesignProcedurePageInfo.getList());
        pageUtils.setTotal(manufactureDesignProcedurePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> recheckPage2(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("first_kind_id",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("thirdKindId",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("register_time",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("register_time",vo.getSearch_registerTime2());
            }
        }
        // 额外添加2个条件
        // 审核状态：审核未通过
        queryWrapper.eq("check_tag","S001-1");
        // 删除状态：未删除
        queryWrapper.eq("change_tag","B002-0");

        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureDesignProcedure> manufactureDesignProcedureList =this.list(queryWrapper);
        PageInfo<ManufactureDesignProcedure> manufactureDesignProcedurePageInfo = new PageInfo<>(manufactureDesignProcedureList);

        //将pageinfo 转换成 pageutils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(manufactureDesignProcedurePageInfo.getList());
        pageUtils.setTotal(manufactureDesignProcedurePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> procedureListPage2(DesignFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<ManufactureDesignProcedure> wrapper = new QueryWrapper<ManufactureDesignProcedure>();
        //审核状态：审核通过
        wrapper.eq("check_Tag", "S001-1");
        //变更状态: 未变更
        wrapper.eq("design_module_Tag","G002-1");
        if(vo != null) {
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                wrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                wrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                wrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                wrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if(!com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                wrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureDesignProcedure> manufactureDesignProcedures = this.list(wrapper);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<ManufactureDesignProcedure>(manufactureDesignProcedures);
        //PageInfo转PageUtils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<ManufactureDesignProcedure>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }
}
